package com.example.app.components.MyModalBottomSheet

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp

@Composable
fun MyCustomModalBottomSheet(
        show: Boolean = false,
        bottomSheetBackgroundColor: Color = MaterialTheme.colorScheme.surface,
        topContent: (@Composable ColumnScope.() -> Unit)? = null,
        centerContent: (@Composable ColumnScope.() -> Unit)? = null,
        bottomContent: (@Composable ColumnScope.() -> Unit)? = null,
        hasNavBar: Boolean? = false,
        hasStatusBar: Boolean? = false,
    //点击背板触发，触发关闭
        backModalClickHandler: () -> Unit,
    //内容区的高度
        contentHeightFloat: Float? = .75f
) {
    var density = LocalDensity.current
    var windowWidth = LocalConfiguration.current.screenWidthDp
    var windowHeight = LocalConfiguration.current.screenHeightDp
    var pxBottomNavicationBarHeight = WindowInsets.navigationBars.getBottom(density)
    var bottomNavicationBarHeight = with(density) {
        if (hasNavBar === false) {
            -pxBottomNavicationBarHeight.toDp()
        } else {
            pxBottomNavicationBarHeight.toDp()
        }
    }
    var offsetY =
        animateDpAsState(targetValue = if (show) windowHeight.dp * (1f - contentHeightFloat!!) - bottomNavicationBarHeight else windowHeight.dp)
    var alpha = animateFloatAsState(targetValue = if (show) 1f else 0f)

    if (offsetY.value < windowHeight.dp) {
        Box(modifier = Modifier
            .fillMaxSize()
            //阻止点击背后的东西
            .pointerInput(Unit) {
                awaitPointerEventScope {
                    while (true) {
                        awaitPointerEvent()
                    }
                }
            }) {
            Box(modifier = Modifier
                .background(Color.Black.copy(.2f))
                .fillMaxSize()
                .clickable {
                    backModalClickHandler()
                }) {}

            Column(
                modifier = Modifier
                    .width(windowWidth.dp)
                    .offset(y = offsetY.value)
                    .height(windowHeight.dp * contentHeightFloat!!)
                    .background(bottomSheetBackgroundColor)
                    .alpha(alpha.value)
                    //阻止点击背后的东西
                    .pointerInput(Unit) {
                        awaitPointerEventScope {
                            while (true) {
                                awaitPointerEvent()
                            }
                        }
                    },
            ) {
                Column(modifier = Modifier.fillMaxSize()) {
                    if (topContent != null) {
                        Column() {
                            topContent()
                        }
                    }
                    if (centerContent != null) {
                        Column(modifier = Modifier.weight(1f)) {
                            centerContent()
                        }
                    }
                    if (bottomContent != null) {
                        Column() {
                            bottomContent()
                        }
                    }
                }
            }
        }
    }
}